#!/usr/bin/ksh

HOSTN=`hostname -s`
#RECIPIENT=q01adm@aero.bombardier.com
RECIPIENT=German.Kudinov@aero.bombardier.com
CLUSTERFLAG=0


echo "\n\n#Check bootdisks and devices. Please wait upto 1-2 minutes"
BOOTDSK=""
BDI=$(bootinfo -b)
echo Bootdisk is $BDI
if [ $(lspv| grep  " rootvg " |grep "$BDI "| wc -l) -eq 0 ] ; then
echo  "Last boot disk $BDI is not in rootvg"
BOOTDSK="Last boot disk $BDI is not in rootvg\n"
fi
echo "\nVerifying bootable image on disk $BDI. Please wait upto 1-2 minutes. Started at `date +%H:%M`"
bosboot -ad /dev/$BDI >/dev/null 2>&1
RC=$?
echo `date +%H:%M` Verified!
if [ $RC -gt 0 ] ; then
echo "Error to validate bootable disk $BDI"
BOOTDSK=$BOOTDSK"Error to validate bootable disk $BDI\n"
fi
echo "\nVerifying bootable image on /dev/ipldevice. Please wait upto 1-2 minutes. Started at `date +%H:%M`"
bosboot -ad /dev/ipldevice >/dev/null 2>&1
RC=$?
echo `date +%H:%M` Verified!
if [ $RC -gt 0 ] ; then
echo "Error to validate bootable device /dev/ipldevice"
BOOTDSK=$BOOTDSK"Error to validate bootable device /dev/ipldevice\n"
fi

if [ -e /tmp/disks_check.flag ] ; then
echo "Someone appears is running the script now as well. If not delete /tmp/disks_check.flag file and retry"
exit 0
fi

touch /tmp/disks_check.flag

OUTLINE=$HOSTN"|"

# Dump list of disks
/usr/sbin/lspv > /tmp/lspv.mon.tmp
# Dump VG details
for i in `lsvg -o` ; do lsvg -L $i > /tmp/$i.tmp ; done

#Check if it is cluster node and set CLUSTERFLAG=1
RC=$(lsvg | grep -cvE "rootvg|oravg|usrsapvg|sapvg|temp")
if [ $RC -gt 0 ] ; then
CLUSTERFLAG=1
fi

# Part for clusters only
if [ $CLUSTERFLAG -eq 1 ] ; then

#Check VGs MAX PPs per VG is enough to have 3rd copy
echo "\n\n#Check VGs MAX PPs per VG is enough to have 3rd copy"

rm /tmp/maxpppervg.flag 2>/dev/null
rm /tmp/maxpppervg.rep 2>/dev/null
for i in `lsvg -o` ; do 
RC=$(grep -E 'MAX PPs per VG|TOTAL PPs' /tmp/$i.tmp| paste - - | awk '{if (1.34*$6 >= $13){print "'$i' MAX PPs per VG is not enough to have 3rd copy"}}'| tee -a /tmp/maxpppervg.rep | wc -l)
printf "."
if [ $RC -gt 0 ] ; then
touch /tmp/maxpppervg.flag
fi
done
if [ -e /tmp/maxpppervg.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/maxpppervg.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

#Check VGs are in Enhanced-Concurent mode
echo "\n\n#Check VGs are in Enhanced-Concurent mode"

rm /tmp/enhconctry.flag 2>/dev/null
rm /tmp/enhconctry.rep 2>/dev/null
for i in `lsvg -o| grep -vE "rootvg|usrsapvg|sapvg"` ; do 
RC=$(grep -ci 'enh' /tmp/$i.tmp 2>/dev/null  )
printf "."
if [ $RC -eq 0 ] ; then
touch /tmp/enhconctry.flag
echo "$i has no Enhanced Concurent mode" >> /tmp/enhconctry.rep
fi
done
if [ -e /tmp/enhconctry.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/enhconctry.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

fi 
# End of part for clusters

#Check VGs disks read access
echo "\n\n#Check VGs disks read access"
rm /tmp/vgsreadaccess.flag 2>/dev/null
rm /tmp/vgsreadaccess.rep 2>/dev/null
for i in `lsvg ` ; do 
grep $i /tmp/lspv.mon.tmp |awk '{print $1}'|while read j; do
printf "."
lqueryvg -Atp $j >/dev/null 
RC=$?
if [ $RC -ne 0 ]; then
   touch /tmp/vgsreadaccess.flag
   echo "$i has an access problem to disk $j" >> /tmp/vgsreadaccess.rep
fi
done
done
if [ -e /tmp/vgsreadaccess.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/vgsreadaccess.rep
echo "Verification was done using a command lqueryvg -Atp <diskname> "
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

#Check stale pps
echo "\n\n#Check stale pps"
rm /tmp/stalepps.flag 2>/dev/null
rm /tmp/stalepps.rep 2>/dev/null
for i in `lsvg -o | grep -vE "hbvg|rootvg"`
do
printf "."
RC=$(lsvg -L $i | grep  STALE| awk '{print $6'}| grep -v "^0"| wc -l)
if [ $RC -ne 0 ]; then
   touch /tmp/stalepps.flag
 echo "$i has stale pps" >> /tmp/stalepps.rep
fi
done
if [ -e /tmp/stalepps.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/stalepps.rep
echo "Verify with command lsvg -L <vgname>; STALE value has to be 0"
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

#Check missing disk
echo "\n\n#Check missing disk"
rm /tmp/missingdisk.flag 2>/dev/null
rm /tmp/missingdisk.rep 2>/dev/null
for i in `lsvg -o | grep -vE "hbvg|rootvg"` 
do 
printf "."
RC=$(lsvg -Lp $i | grep -c miss 2>/dev/null  )
if [ $RC -ne 0 ]; then
   touch /tmp/missingdisk.flag
 echo "$i has missing disk" >> /tmp/missingdisk.rep
fi
done

if [ -e /tmp/missingdisk.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/missingdisk.rep
echo "Vrify with command lsvg -Lp <vgname>"
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi


#Check queue_depth
echo "\n\n#Check queue_depth"
rm /tmp/emcqueue_depth.flag 2>/dev/null
rm /tmp/queue_depth.flag 2>/dev/null
rm /tmp/emcqueue_depth.rep 2>/dev/null
rm /tmp/queue_depth.rep 2>/dev/null
for i in `lsvg ` ; do 
grep $i /tmp/lspv.mon.tmp |awk '{print $1}'|while read j; do
printf "."
RC=$(lsattr -El $j | grep queue_depth | awk '{print $2}')
RCC=$(lsdev -l $j | grep -c PowerPath)
if [ $RC -lt 256 -a $RCC -ne 0 ]; then
   touch /tmp/emcqueue_depth.flag
   echo "$i has a EMC disk $j with queue_depth value $RC; it has to be 256" >> /tmp/emcqueue_depth.rep
fi
if [ $RC -lt 20 -a $RCC -eq 0 ]; then
   touch /tmp/queue_depth.flag
   echo "$i has a disk $j with queue_depth value $RC; it has to be 20 or more" >> /tmp/queue_depth.rep
fi

done
done
if [ -e /tmp/emcqueue_depth.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/emcqueue_depth.rep
else
#OUTLINE=$OUTLINE"|"
if [ $RCC -ne 0 ] ; then
echo "EMC disks are Verified!"
fi
fi
if [ -e /tmp/queue_depth.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/queue_depth.rep
else
#OUTLINE=$OUTLINE"|"
echo "Non EMC disks are Verified!"
fi

#Check Dead paths on FC adapter
echo "\n\n#Check Dead paths on FC adapter "
rm /tmp/deadpaths.flag 2>/dev/null
rm /tmp/deadpaths.rep 2>/dev/null
RC=$(lsdev| grep fscsi| grep -c Available)
if [ $RC -gt 0 ] ; then
RCC=$(powermt display| grep fscsi| awk '{if($5 > 0){print "Dead paths on $1"}}' | tee -a /tmp/deadpaths.rep | wc -l)
if [ $RCC -gt 0 ] ; then
touch /tmp/deadpaths.flag
echo "Verify with SAN and fix if required with command powermt check" >> /tmp/deadpaths.rep
fi
fi
if [ -e /tmp/deadpaths.flag    ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/deadpaths.rep   
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi


#Check FC adapter no reservation
echo "\n\n#Check FC adapter no reservation"
rm /tmp/fcadapterres.flag 2>/dev/null
rm /tmp/fcadapterres.rep 2>/dev/null
RC=$(lsdev| grep fscsi| grep -c Available)
if [ $RC -gt 0 ] ; then
RCC=$(lsdev| grep fscsi| grep Available| awk '{print $1 }' |  while read line ; do echo $line; lsattr -El $line | grep -E 'dyntrk|fc_err_recov' | awk '{print $1" "$2 }' ; done | paste - - - | grep -vcE "yes|fast")
if [ $RCC -gt 0 ] ; then
touch /tmp/fcadapterres.flag
echo "FC adapter has wrong reservation policy or dyntrk setting; it has to be dyntrk=yes and fc_err_recov=fast_fail" >> /tmp/fcadapterres.rep
fi
fi
if [ -e /tmp/fcadapterres.flag ] ; then
#OUTLINE=$OUTLINE" to fix |"
echo
cat /tmp/fcadapterres.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

#Check nfs mounts to external systems
echo "\n\n#Check nfs mounts to external systems"
rm /tmp/nfmount.flag 2>/dev/null
rm /tmp/nfmount.rep 2>/dev/null
if [ -e /usr/local/etc/nfs2monitor.txt ] ; then
for i in `cat /usr/local/etc/nfs2monitor.txt` ; do
printf "."
RC=$(mount | grep nfs | grep "$i " 2>/dev/null | wc -l )
if [ $RC -ne 1 ] ; then
echo "Check nfs mount of $i on `hostname -s`.\nCompare file /usr/local/etc/nfs2monitor.txt and command mount|grep nfs" >> /tmp/nfmount.rep
break
fi
done
if [ -e /tmp/nfmount.flag ] ; then
echo
cat /tmp/nfmount.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi
else
echo "File with the list of nfs mounts to external systems (/usr/local/etc/nfs2monitor.txt) should exist.\nSkipping"
fi

#Check saposcol is running
echo "\n\n#Check saposcol is running"
rm /tmp/choscol.flag 2>/dev/null
rm /tmp/choscol.rep  2>/dev/null
RC=$( ps -ef| grep saposcol| grep -v grep | grep -v check_saposcol | wc -l )
if [ $RC -eq 0 ] ; then
echo "saposcol is not running" >> /tmp/choscol.rep
fi
if [ -e /tmp/choscol.flag ] ; then
echo
cat /tmp/choscol.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

#Check ccms agent is running
echo "\n\n#Check ccms agent is running"
rm /tmp/sapccm.flag 2>/dev/null
rm /tmp/sapccm.rep  2>/dev/null
RC=$( ps -ef| grep sapccm | grep -v grep | wc -l )
if [ $RC -eq 0 ] ; then
echo "ccms agent is not running" >> /tmp/sapccm.rep
fi
if [ -e /tmp/sapccm.flag  ] ; then
echo
cat /tmp/sapccm.rep
else
#OUTLINE=$OUTLINE"|"
echo "Verified!"
fi

list="/tmp/maxpppervg.rep
/tmp/enhconctry.rep
/tmp/vgsreadaccess.rep
/tmp/stalepps.rep
/tmp/missingdisk.rep
/tmp/emcqueue_depth.rep
/tmp/queue_depth.rep
/tmp/deadpaths.rep
/tmp/fcadapterres.rep
/tmp/nfmount.rep
/tmp/choscol.rep
/tmp/sapccm.rep"
RC=$((echo $BOOTDSK; ls `echo $list` 2>/dev/null)| wc -l)
echo "===================================================================="
# Verifying sysdumpdevice
/usr/local/bin/sysdumpinfo.sh| sed 's/Configuration ok/Sysdump device is ok/g'
# Verifying cio mount options
RC0=$(mount|grep -i oracle| grep -ic cio)
if [ $RC0 -gt 1 ] ; then
echo "CIO mount option might be not properly setup"
mount|grep -i oracle| grep -i cio
fi
if [ $RC -gt 0 ] ; then
echo "===================================================================="
echo "There is misconfiguration found. Please check:"
cat `echo $list`  2>/dev/null
echo $BOOTDSK
echo "===================================================================="
else
echo "===================================================================="
echo "Script has not found misconfiguration in the verified areas"
echo "===================================================================="
fi

rm /tmp/disks_check.flag


